package com.javaGather.day2;

import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.Objects;
import java.util.Scanner;

public class StudentManage {

    private static ArrayList<Student> students = new ArrayList<Student>();

    //
    private static boolean isRun = true;

    /**
     * 学生管理系统
     *
     * @param args
     */
    public static void main(String[] args) {
        while (true) {
            //主界面
            System.out.println("-----------欢迎来到学生管理系统----------");
            System.out.println("1.新增学生");
            System.out.println("2.修改学生");
            System.out.println("3.删除学生");
            System.out.println("4.查询学生");
            System.out.println("5.退出系统");
            System.out.println("请选择1-5");
            System.out.println("-------------------------------------");

            Scanner scanner = new Scanner(System.in);
            switch (scanner.nextInt()) {
                case 1:
                    addStudent();
                    break;
                case 2:
                    updateStudent();
                    break;
                case 3:
                    deleteStudent();
                    break;
                case 4:
                    showStudent();
                    break;
                case 5:
                    /**
                     * 退出三种方式
                     */
//                    isRun = false;
//                    return;
                    System.exit(0);
            }
        }


    }


    //查询
    public static void showStudent() {
        if (!students.isEmpty()) {
            System.out.println("-------------------------------------");
            System.out.println("学号\t姓名\t年龄\t地址\t");
            for (Student student : students) {
                System.out.println(student.getId() + "\t" + student.getName() + "\t" + student.getAge() + "\t" + student.getAddress() + "\t");
            }
            System.out.println("-------------------------------------");
            return;
        }
        System.out.println("暂无数据");
        System.out.println("-------------------------------------");

    }

    //添加
    public static void addStudent() {

        Scanner scanner = new Scanner(System.in);
        System.out.println("-------------------------------------");
        System.out.println("请输入学生学号:");
        String id = scanner.nextLine();

        //判断学号是否存在
        Student stu = getById(id);
        if (stu != null) {
            System.out.println("不能够插入相同发的学号");
            System.out.println("-------------------------------------");
            return;
        }

        System.out.println("请输入学生姓名:");
        String name = scanner.nextLine();

        System.out.println("请输入学生地址:");
        String address = scanner.nextLine();

        System.out.println("请输入学生年龄:");
        int age = scanner.nextInt();

        students.add(new Student(id, name, age, address));
        System.out.println("新增完成！");
        System.out.println("-------------------------------------");
    }

    //根据Id查学生信息
    public static Student getById(String id) {
        return getById(id, false);
    }

    //根据Id查学生信息并删除
    public static Student getById(String id, boolean isDelete) {
        for (Student student : students) {
            if (id.equals(student.getId())) {
                if (isDelete) {
                    students.remove(student);
                    System.out.println("删除成功");
                    System.out.println("-------------------------------------");
                }
                return student;
            }
            System.out.println("学生不存在");
            System.out.println("-------------------------------------");
        }
        return null;
    }

    //删除学生
    public static void deleteStudent() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("-------------------------------------");
        System.out.println("请输入学生学号:");
        String id = scanner.nextLine();
        getById(id, true);
    }

    //删除学生
    public static void updateStudent() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("-------------------------------------");
        System.out.println("请输入修改学生学号:");
        String id = scanner.nextLine();
        Student stu = getById(id, false);
        if (stu == null) {
            System.out.println("没有找到该学生信息");
            System.out.println("-------------------------------------");
            return;
        }
        //修改信息
        System.out.println("请输入学生姓名:");
        String newName = scanner.nextLine();
        stu.setName(newName);
        System.out.println("请输入学生地址:");
        String newAddress = scanner.nextLine();
        stu.setAddress(newAddress);
        System.out.println("请输入学生年龄:");
        int newAge = scanner.nextInt();
        stu.setAge(newAge);
        System.out.println("修改成功");
        System.out.println("-------------------------------------");

    }

}
